$(function(){
    $('#list').mothinTreegrid({
        url:'system/organization!queryOrganization.action',
        treeField:'organizationName',
        columns:[[
            {field:'organizationName',title:'机构名称',width:180,fixed:true}, 
            {field:'manager',title:'负责人',width:150,fixed:true,sortable:true,formatter :function(value){
                if(value){
                    return value;
                    }
                return "-";
            }}, 
            {field:'memo',title:'备注',width:200},
            {field:'id',title:'操作',width:0,fixed:true,resizable:true,formatter:function(value,row){
                var data=frame_queryOperation(History.getState().url);
                if(data&&data.rows&&data.rows.length>0){
                    var link="";
                    for(var i=0;i<data.rows.length;i++){
                        link+='<a class="btn btn-link btn-xs" onclick="handle(\''+data.rows[i].id+'\',\''+data.rows[i].action+'\',\''+data.rows[i].name+'\',\''+value+'\');"><span class="'+data.rows[i].ico+'" />&nbsp;'+data.rows[i].name+'</a>';
                    }
                    return link;
                }
            }}
        ]]
    },handle);
});

function handle(id,action,name,bizId){
    switch(action){
    case 'system/organization!addOrganization.action':
        showDialog(action,'添加新机构',800,400,function(){
            $("#list").treegrid("reload");
        });
        break;
    case 'system/organization!addOrganizationchildren.action':
        var url=action+'?organization.parentId='+bizId;
        showDialog(url,'添加新机构',800,400,function(){
            $("#list").treegrid("reload");
        });
        break;
    case 'system/organization!updateOrganization.action':
        var url=action+'?organization.id='+bizId;
        showDialog(url,'修改机构信息',800,400,function(){
            $("#list").treegrid("reload");
        });
        break;
    case 'system/organization!showManager.action':
        var url="system/organization!selectManager.action?organization.id="+bizId;
        $.ajax({
            url: url,
            dataType:'json',
            success:function(data) {
                if(data&&data.length==0) {
                    showMessage("该部门暂时没人!","","warning");
                }
                else{
                    url=action+'?organization.id='+bizId;
                    showDialog(url,'选择负责人',800,400,function(){
                        $("#list").treegrid("reload");
                    });
                }
            }
        });
        break;
    case 'system/organization!deleteOrganization.action':
        showConfirm('确认删除此机构吗？',function(){
            $.ajax({
                type: 'POST',
                url: action,
                data: {'organization.id':bizId},
                success: function(data){
                    if(data.ok){
                        showMessage('角色删除成功',function(){
                            $("#list").treegrid("reload");
                        });
                    }else{
                        showMessage('角色删除失败：'+data.message,null,'error');
                    }
                }
            });
        });
        break;
    }
}
